home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / games / nhak_src.zip / GETLINE.C < prev    next >
C/C++ Source or Header  |  1993-03-16  |  8KB  |  389 lines

  1. /*    SCCS Id: @(#)getline.c    3.0    89/06/16
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3. /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5. #include "hack.h"
  6. #include "func_tab.h"
  7.  
  8. /*
  9.  * Some systems may have getchar() return EOF for various reasons, and
  10.  * we should not quit before seeing at least NR_OF_EOFS consecutive EOFs.
  11.  */
  12. #if defined(SYSV) || defined(DGUX)
  13. #define    NR_OF_EOFS    20
  14. #endif
  15. #ifdef MACOS
  16. extern short macflags;
  17. #endif
  18. #ifdef OVL1
  19.  
  20. char morc = 0;    /* tell the outside world what char he used */
  21.  
  22. #endif /* OVL1 */
  23.  
  24. extern char erase_char, kill_char;    /* from appropriate tty.c file */
  25.  
  26. #ifdef OVL1
  27.  
  28. /*
  29.  * Read a line closed with '\n' into the array char bufp[BUFSZ].
  30.  * (The '\n' is not stored. The string is closed with a '\0'.)
  31.  * Reading can be interrupted by an escape ('\033') - now the
  32.  * resulting string is "\033".
  33.  */
  34. void
  35. getlin(bufp)
  36. register char *bufp;
  37. {
  38.     register char *obufp = bufp;
  39.     register int c;
  40. #ifdef MACOS
  41.     short    tmpflags;
  42.     
  43.     tmpflags = macflags;
  44.     macflags &= ~fDoNonKeyEvt;
  45. #endif
  46.     flags.toplin = 2;        /* nonempty, no --More-- required */
  47.     for(;;) {
  48.         (void) fflush(stdout);
  49.         if((c = Getchar()) == EOF) {
  50.             *bufp = 0;
  51. #ifdef MACOS
  52.     macflags = tmpflags;
  53. #endif
  54.             return;
  55.         }
  56.         if(c == '\033') {
  57.             *obufp = c;
  58.             obufp[1] = 0;
  59. #ifdef MACOS
  60.     macflags = tmpflags;
  61. #endif
  62.             return;
  63.         }
  64.         if(c == erase_char || c == '\b') {
  65.             if(bufp != obufp) {
  66.                 bufp--;
  67.                 putstr("\b \b");/* putsym converts \b */
  68.             } else    bell();
  69.         } else if(c == '\n') {
  70.             *bufp = 0;
  71. #ifdef MACOS
  72.     macflags = tmpflags;
  73. #endif
  74.             return;
  75.         } else if(' ' <= c && c < '\177' && 
  76.                 (bufp-obufp < BUFSZ-1 || bufp-obufp < COLNO)) {
  77.                 /* avoid isprint() - some people don't have it
  78.                    ' ' is not always a printing char */
  79.             *bufp = c;
  80.             bufp[1] = 0;
  81.             putstr(bufp);
  82.             bufp++;
  83.         } else if(c == kill_char || c == '\177') { /* Robert Viduya */
  84.                 /* this test last - @ might be the kill_char */
  85.             while(bufp != obufp) {
  86.                 bufp--;
  87.                 if(curx == 1 && cury > 1) {
  88.                     putstr("\b \b\b");
  89.                     curx = CO;
  90.                 } else putstr("\b \b");
  91.             }
  92.         } else
  93.             bell();
  94.     }
  95. #ifdef MACOS
  96.     macflags = tmpflags;
  97. #endif
  98. }
  99.  
  100. #endif /* OVL1 */
  101. #ifdef OVLB
  102.  
  103. void
  104. getret() {
  105.     cgetret("");
  106. }
  107.  
  108. void
  109. cgetret(s)
  110. register const char *s;
  111. {
  112.     putsym('\n');
  113.     if(flags.standout)
  114.         standoutbeg();
  115.     putstr("Hit ");
  116.     putstr(flags.cbreak ? "space" : "return");
  117.     putstr(" to continue: ");
  118.     if(flags.standout)
  119.         standoutend();
  120.     xwaitforspace(s);
  121. }
  122.  
  123. #endif /* OVLB */
  124. #ifdef OVL1
  125.  
  126. void
  127. xwaitforspace(s)
  128. register const char *s;    /* chars allowed besides space or return */
  129. {
  130.     register int c;
  131. #ifdef MACOS
  132.     short    tmpflags;
  133. #endif
  134.  
  135.     morc = 0;
  136. #ifdef MACOS
  137.     flags.wantspace = TRUE;
  138.     tmpflags = macflags;
  139.     macflags &= ~fDoNonKeyEvt;
  140.     HideCursor();
  141. #endif
  142.  
  143.     while((c = readchar()) != '\n') {
  144. #ifdef MACOS
  145.         if(c == '\r' || c == 0x3 || c == 'p') break;
  146. #endif
  147.         if(flags.cbreak) {
  148.             if(c == ' ') break;
  149.             if(s && index(s,c)) {
  150.                 morc = c;
  151.                 break;
  152.             }
  153.             bell();
  154.         }
  155.     }
  156.  
  157. #ifdef MACOS
  158.     ShowCursor();
  159.     flags.wantspace = FALSE;
  160.     macflags = tmpflags;
  161. #endif
  162. }
  163.  
  164. #endif /* OVL1 */
  165. #ifdef OVL0
  166.  
  167. static int NEARDATA last_multi;
  168.  
  169. char *
  170. parse()
  171. {
  172. #ifdef LINT    /* static char in_line[COLNO]; */
  173.     char in_line[COLNO];
  174. #else
  175.     static char in_line[COLNO];
  176. #endif
  177.     register int foo, cnt = 0;
  178.     boolean prezero = FALSE;
  179.  
  180.     multi = 0;
  181.     flags.move = 1;
  182.     curs_on_u();
  183.  
  184.     if (!flags.num_pad || (foo = readchar()) == 'n')
  185.         do {
  186.             foo = readchar();
  187.             if(foo >= '0' && foo <= '9') {
  188.                 multi = 10*multi+foo-'0';
  189.                 if (multi < 0 || multi > LARGEST_INT)
  190.                     multi = LARGEST_INT;
  191.                 if (multi > 9) {
  192.                     remember_topl();
  193.                     home();
  194.                     cl_end();
  195.                     Printf("Count: %d", multi);
  196.                 }
  197.                 last_multi = multi;
  198.                 if(!cnt && foo == '0') prezero = TRUE;
  199.                 cnt++;
  200.             }
  201.             if (foo == '\033') {   /* esc cancels count (TH) */
  202.                 remember_topl();
  203.                 home();
  204.                 cl_end();
  205.                 multi = last_multi = 0;
  206.             }
  207.         } while(foo >= '0' && foo <= '9');
  208. # ifdef REDO
  209.     if (foo == DOAGAIN || in_doagain)
  210.         multi = last_multi;
  211.     else {
  212.         savech(0);    /* reset input queue */
  213.         savech(foo);
  214.     }
  215. # endif
  216.     if(multi) {
  217.         multi--;
  218.         save_cm = in_line;
  219.     }
  220.     in_line[0] = foo;
  221.     in_line[1] = 0;
  222.     if(foo == 'g' || foo == 'G' || (flags.num_pad && foo == '5')){
  223.         in_line[1] = Getchar();
  224. #ifdef REDO
  225.         savech(in_line[1]);
  226. #endif
  227.         in_line[2] = 0;
  228.     }
  229.     if(foo == 'm' || foo == 'M'){
  230.         in_line[1] = Getchar();
  231. #ifdef REDO
  232.         savech(in_line[1]);
  233. #endif
  234.         in_line[2] = 0;
  235.     }
  236.     clrlin();
  237.     if(prezero) in_line[0] = '\033';
  238.     return(in_line);
  239. }
  240.  
  241. #endif /* OVL0 */
  242. #ifdef OVLB
  243.  
  244. #ifdef UNIX
  245. static void
  246. end_of_input()
  247. {
  248.     settty("End of input?\n");
  249.     clearlocks();
  250.     exit(0);
  251. }
  252. #endif
  253.  
  254. #endif /* OVLB */
  255. #ifdef OVL0
  256.  
  257. char
  258. readchar() {
  259.     register int sym;
  260.  
  261.     (void) fflush(stdout);
  262. #ifdef UNIX
  263.     if((sym = Getchar()) == EOF)
  264. # ifdef NR_OF_EOFS
  265.     { /*
  266.        * Some SYSV systems seem to return EOFs for various reasons
  267.        * (?like when one hits break or for interrupted systemcalls?),
  268.        * and we must see several before we quit.
  269.        */
  270.         register int cnt = NR_OF_EOFS;
  271.         while (cnt--) {
  272.             clearerr(stdin);    /* omit if clearerr is undefined */
  273.             if((sym = Getchar()) != EOF) goto noteof;
  274.         }
  275.         end_of_input();
  276.          noteof:    ;
  277.     }
  278. # else
  279.         end_of_input();
  280. # endif /* NR_OF_EOFS /**/
  281. #else
  282.     sym = Getchar();
  283. #endif /* UNIX */
  284.     if(flags.toplin == 1)
  285.         flags.toplin = 2;
  286.     return((char) sym);
  287. }
  288.  
  289. #endif /* OVL0 */
  290. #ifdef OVL2
  291.  
  292. #ifdef COM_COMPL
  293. /* Read in an extended command - doing command line completion for
  294.  * when enough characters have been entered to make a unique command.
  295.  * This is just a modified getlin().   -jsb
  296.  */
  297. void
  298. get_ext_cmd(bufp)
  299. register char *bufp;
  300. {
  301.     register char *obufp = bufp;
  302.     register int c;
  303.     int com_index, oindex;
  304. #ifdef MACOS
  305.     short tmpflags;
  306.     
  307.     tmpflags = macflags & ~(fExtCmdSeq1 | fExtCmdSeq2 | fExtCmdSeq3);
  308.     macflags &= ~fDoNonKeyEvt;
  309. #endif
  310.  
  311.     flags.toplin = 2;        /* nonempty, no --More-- required */
  312.  
  313.     for(;;) {
  314.         (void) fflush(stdout);
  315.         if((c = readchar()) == EOF) {
  316.             *bufp = 0;
  317. #ifdef MACOS
  318.             macflags = tmpflags;
  319. #endif
  320.             return;
  321.         }
  322.         if(c == '\033') {
  323.             *obufp = c;
  324.             obufp[1] = 0;
  325. #ifdef MACOS
  326.             macflags = tmpflags;
  327. #endif
  328.             return;
  329.         }
  330.         if(c == erase_char || c == '\b') {
  331.             if(bufp != obufp) {
  332.                 bufp--;
  333.                 putstr("\b \b"); /* putsym converts \b */
  334.             } else    bell();
  335.         } else if(c == '\n') {
  336.             *bufp = 0;
  337. #ifdef MACOS
  338.             macflags = tmpflags;
  339. #endif
  340.             return;
  341.         } else if(' ' <= c && c < '\177') {
  342.                 /* avoid isprint() - some people don't have it
  343.                    ' ' is not always a printing char */
  344.             *bufp = c;
  345.             bufp[1] = 0;
  346.             oindex = 0;
  347.             com_index = -1;
  348.  
  349.             while(extcmdlist[oindex].ef_txt != NULL){
  350.                 if(!strncmp(obufp, extcmdlist[oindex].ef_txt,
  351.                     strlen(obufp)))
  352.                     if(com_index == -1) /* No matches yet*/
  353.                         com_index = oindex;
  354.                     else /* More than 1 match */
  355.                         com_index = -2;
  356.                 oindex++;
  357.             }
  358.             if(com_index >= 0){
  359.                 Strcpy(obufp, extcmdlist[com_index].ef_txt);
  360.                 /* finish printing our string */
  361.                 putstr(bufp);
  362.                 bufp = obufp; /* reset it */
  363.                 if(strlen(obufp) < BUFSIZ-1 &&
  364.                  strlen(obufp) < COLNO)
  365.                     /* set bufp at the end of our string */
  366.                     bufp += strlen(obufp);
  367.             } else {
  368.                 putstr(bufp);
  369.                 if(bufp-obufp < BUFSZ-1 && bufp-obufp < COLNO)
  370.                     bufp++;
  371.             }
  372.         } else if(c == kill_char || c == '\177') { /* Robert Viduya */
  373.                 /* this test last - @ might be the kill_char */
  374.             while(bufp != obufp) {
  375.                 bufp--;
  376.                 putstr("\b \b");
  377.             }
  378.         } else
  379.             bell();
  380.     }
  381. #ifdef MACOS
  382.     macflags = tmpflags;
  383. #endif
  384.  
  385. }
  386. #endif /* COM_COMPL */
  387.  
  388. #endif /* OVL2 */
  389.